package net.entelijan.cobean.progress;

import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JProgressBar;

import net.entelijan.cobean.core.ICobean;
import net.entelijan.cobean.core.IInitializer;
import net.entelijan.cobean.core.impl.AbstractBoundPropertyValueComponentBean;

// TODO Make some testcases for that cobean. 
public class DefaultProgressInitializer implements
		IInitializer<ICobean<Integer>> {

	private static final class DefaultBoundPropertyValueComponentBean extends
			AbstractBoundPropertyValueComponentBean<Integer> {
		private final DefaultBoundedRangeModel rangeModel;

		private DefaultBoundPropertyValueComponentBean(
				DefaultBoundedRangeModel rangeModel) {
			this.rangeModel = rangeModel;
		}

		@Override
		protected Integer getValuePlain() {
			return rangeModel.getValue();
		}

		@Override
		protected void setValuePlain(Integer value) {
			// Put the value into the range of 0 and 100
			int val;
			if (value == null) {
				val = 0;
			} else if (value < 0) {
				val = 0;
			} else if (value > 100) {
				val = 100;
			} else {
				val = value;
			}
			rangeModel.setValue(val);
		}
	}

	private JProgressBar coProgress;

	public DefaultProgressInitializer() {
		super();
	}

	@Override
	public ICobean<Integer> initialize() {
		final DefaultBoundedRangeModel rangeModel = new DefaultBoundedRangeModel(
				0, 0, 0, 100);
		coProgress.setModel(rangeModel);

		AbstractBoundPropertyValueComponentBean<Integer> re = new DefaultBoundPropertyValueComponentBean(rangeModel);
		return re;
	}

	public JProgressBar getCoProgress() {
		return coProgress;
	}

	public void setCoProgress(JProgressBar coProgress) {
		this.coProgress = coProgress;
	}

}
